package com.syh.jn.scm.service.impl.supplier.base.goods;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Goods;
import com.syh.jn.scm.domain.supplier.event.GoodsEvent;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsRepository;
import com.syh.jn.scm.dto.supplier.base.goods.GoodsMto;
import com.syh.jn.scm.dto.supplier.base.goods.GoodsSto;
import com.syh.jn.scm.dto.supplier.base.goods.GoodsVo;
import com.syh.jn.scm.service.api.supplier.base.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品
 */
@Service
@Transactional
public class GoodsServiceImpl extends AbstractServiceImpl<GoodsSto, GoodsMto, GoodsVo, Goods, Long> implements GoodsService {

  @Autowired
  private GoodsRepository goodsRepositoryImpl;

  @Override
  protected AggregateRepository<Goods, Long> getAggregateRepository() {
    return goodsRepositoryImpl;
  }

  /**
   * 保存商品
   * @param sto
   * @return
   */
  @Override
  public Long save(GoodsSto sto) {
    Goods goods = Goods.createGoods(sto);
    //添加商品
    Long id = goodsRepositoryImpl.add(goods);

    Goods goodsMq = goodsRepositoryImpl.getById(id);
    Object[] obj = new Object[]{goodsMq};
    //发送事件消息
    this.publicEvent(GoodsEvent.buildGoodsSave(obj));
    return id;
  }

  /**
   * 修改商品
   * @param mto
   */
  @Override
  public void update(GoodsMto mto) {
    Goods goods = goodsRepositoryImpl.getById(mto.getId());
    goods.modify(mto);
    goodsRepositoryImpl.update(goods);

    Goods goodsMq = goodsRepositoryImpl.getById(mto.getId());
    Object[] obj = new Object[]{goodsMq};
    //发送事件消息
    this.publicEvent(GoodsEvent.buildGoodsUpdate(obj));
  }

  /**
   * 删除商品
   * @param id
   */
  @Override
  public void delete(Long id) {
    Goods goods = goodsRepositoryImpl.getById(id);
    goods.destroy();
    goodsRepositoryImpl.remove(goods);

    Object[] obj = new Object[]{goods};
    //发送事件消息
    this.publicEvent(GoodsEvent.buildGoodsDel(obj));
  }

  /**
   * 启用商品
   * @param id
   */
  @Override
  public void enabled(Long id) {
    Goods goods = goodsRepositoryImpl.getById(id);
    goods.enabled();
    goodsRepositoryImpl.update(goods);

    Object[] obj = new Object[]{goods};
    //发送事件消息
    this.publicEvent(GoodsEvent.buildGoodsUpdate(obj));
  }

  /**
   * 停用商品
   * @param id
   */
  @Override
  public void disabled(Long id) {
    Goods goods = goodsRepositoryImpl.getById(id);
    goods.disabled();
    goodsRepositoryImpl.update(goods);

    Object[] obj = new Object[]{goods};
    //发送事件消息
    this.publicEvent(GoodsEvent.buildGoodsUpdate(obj));
  }

  @Override
  public List<Long> batchSave(List<GoodsSto> goodsStos) {
    List<Long> ids = new ArrayList<>();
    goodsStos.stream().forEach(sto -> {
      Long id = this.save(sto);
      ids.add(id);
    });
    return ids;
  }
}
